﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WorkFlowSystem.Common;
using WorkFlowSystem.DataBase;
using DevComponents.AdvTree;

namespace WorkFlowSystem
{
    public partial class DepartmentTreeUi : Form
    {
        public DepartmentTreeUi()
        {
            InitializeComponent();
            m_dt = new DataTable();
        }

        private void DepartmentTreeUi_Load(object sender, EventArgs e)
        {
            DBOperator db_op = new DBOperator();
            if (db_op.ConnectDB() < 0)
            {
                MessageBox.Show("无法连接服务器，请检查网络连接");
                return;
            }
            List<SqlParameter> para_list = new List<SqlParameter>();
            string sql = "select a.did, a.d_name, a.d_uid, a.d_parent, b.d_name as pd_name, c.u_name as u_name, c.uid as u_uid " +
                "from department a left join department b on a.d_parent=b.did left join user_info c on a.d_uid=c.uid";

            if (db_op.ExecuteDataTable(sql, null, false, ref m_dt) < 0)
                return;
            db_op.CloseDB();

            if (m_dt.Rows.Count <= 0)
            {
                Logger.WriteLog("no department has been created yet");
                MessageBox.Show("没有部门信息");
                m_dt.Clear();
                this.Close();
            }
            else
                get_tree();
        }

        private void get_tree()
        {
            for (int i = 0; i < m_dt.Rows.Count; i++) {
                add_nodes(m_dt.Rows[i]);
            }
        }

        private TreeNode add_nodes(DataRow row)
        {
            TreeNode node = null;
            TreeNode p_node = null;
            TreeNode u_node = null;

            if (row["d_parent"].ToString() == "0") {
                //root node
                node = new TreeNode();
                node.Name = row["did"].ToString();
                node.Text = row["d_name"].ToString();

                u_node = new TreeNode();
                u_node.Name = row["u_uid"].ToString();
                u_node.Text = row["u_name"].ToString();
                u_node.ToolTipText = "[ID]" + u_node.Name;
                node.Nodes.Add(u_node);

                dpt_tree.Nodes.Add(node);
                return node;
            }

            node = find_node(row["did"].ToString());
            p_node = find_node(row["d_parent"].ToString());

            if (node == null && p_node == null) {
                int row_idx = find_row(row["d_parent"].ToString());
                if (row_idx >= 0) {
                    p_node = add_nodes(m_dt.Rows[row_idx]);
                }

                node = new TreeNode();
                node.Name = row["did"].ToString();
                node.Text = row["d_name"].ToString();

                u_node = new TreeNode();
                u_node.Name = row["u_uid"].ToString();
                u_node.Text = row["u_name"].ToString();
                u_node.ToolTipText = "[ID]" + u_node.Name;
                node.Nodes.Add(u_node);

                p_node.Nodes.Add(node);
                dpt_tree.Nodes.Add(p_node);
            }
            else if (node == null && p_node != null) {
                node = new TreeNode();
                node.Name = row["did"].ToString();
                node.Text = row["d_name"].ToString();

                u_node = new TreeNode();
                u_node.Name = row["u_uid"].ToString();
                u_node.Text = row["u_name"].ToString();
                u_node.ToolTipText = "[ID]" + u_node.Name;
                node.Nodes.Add(u_node);

                p_node.Nodes.Add(node);
            }
            else if (node != null && p_node == null)
            {
                int row_idx = find_row(row["d_parent"].ToString());
                if (row_idx >= 0)
                {
                    p_node = add_nodes(m_dt.Rows[row_idx]);
                }
                p_node.Nodes.Add(node);
            }
            else {
                Logger.WriteLog("why duplicated row exist!!");
                return null;
            }
            return node;
        }

        private TreeNode find_node(string node_name)
        {
            TreeNode[] nodes = dpt_tree.Nodes.Find(node_name, true);
            if (nodes.Length > 0)
                return nodes[0];
            return null;
        }

        private int find_row(string did)
        {
            for (int i = 0; i < m_dt.Rows.Count; i++) {
                if (m_dt.Rows[i]["did"].ToString() == did)
                    return i;
            }
            return -1;
        }

        private DataTable m_dt;
    }
}
